package com.daxia.ogmall.web.controller.admin;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.ogmall.common.Entry;
import com.daxia.ogmall.dto.GzActivityDTO;
import com.daxia.ogmall.service.BaseExcelCallback;
import com.daxia.ogmall.service.GzActivityService;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.DateUtils;
import com.daxia.ogmall.web.controller.BaseController;

@Controller
@RequestMapping(value = "/admin/gzActivity", produces="text/html;charset=UTF-8")
public class AdminGzActivityController extends BaseController {

	@Autowired
	private GzActivityService gzActivityService;

	@RequestMapping(value = "/detail")
	public String detail(Map<String, Object> map, Long id) {
		if (id != null) {
			GzActivityDTO dto = gzActivityService.load(id);
			map.put("n", dto);
		}
		return "admin/gzActivity/gzActivity_detail";
	}
	
	@ResponseBody
	@RequestMapping(value = "/save")
	@PreAuthorize("hasRole('gzActivity')")
	public String save(GzActivityDTO dto) throws Exception {
		if (dto.getId() == null) {
			gzActivityService.create(dto);
		} else {
			GzActivityDTO old = gzActivityService.load(dto.getId());
			// old.setXXX(dto.getXXX());
			if (1 == 1) {
				throw new RuntimeException("请实现更新逻辑");
			} 
			gzActivityService.updateAllFields(old);
		}
		return ok();
	}

    @PreAuthorize("hasRole('gzActivity')")
	@ResponseBody
	@RequestMapping(value = "/delete") 
	public String delete(Long[] ids) throws IOException {
		gzActivityService.deleteByIds(ids);
		return ok();
	}
	
	@RequestMapping(value = "/list")
	public String list(GzActivityDTO dto, Map<String, Object> map, Page page) {
		List<GzActivityDTO> dtos = gzActivityService.find(dto, page);
		map.put("list", dtos);
		map.put("query", dto);
		return "admin/gzActivity/gzActivity_list";
	}
	
	@ResponseBody
	@RequestMapping(value = "/downExcel")
	public ResponseEntity<byte[]> downExcel() throws IOException {
		String fileName = expExcel();
		if(null!=fileName){
			HttpHeaders headers = new HttpHeaders();
			fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
			headers.setContentDispositionFormData("attachment", fileName);
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			return new ResponseEntity<byte[]>(
					FileUtils
							.readFileToByteArray(new File("/tmp"+"/"
									+ fileName)),
					headers, HttpStatus.CREATED);
		}
		return null;
	}

	public String expExcel() throws IOException {

		List<GzActivityDTO> dtos = gzActivityService.findAll();
			Entry<String, String> excelEntry = toExcel("/tmp/", "user_list", dtos,
					new BaseExcelCallback<GzActivityDTO>() {

						@Override
						public Row header(Sheet sheet) {
							Workbook wb = sheet.getWorkbook();
							CellStyle style = buildHeaderStyle(wb);
							Row row = sheet.createRow(0);
							row.setHeight((short)500);
							int column = 0;
							String[] titles = new String[] { "序号", "手机",
									"姓名", "报名时间" };
							for (String title : titles) {
								CellUtil.createCell(row, column, title, style);
								column += 1;
							}
							return row;
						}

						@Override
						public Row convert(Row row, GzActivityDTO obj) {
							return null;
						}


						@Override
						public Row footer(Sheet sheet) {
							return null;
						}

						@Override
						public Row convert(Workbook wb, Drawing pa, Row row,
								GzActivityDTO obj) {
							row.setHeight((short)500);
							createCell(row, 0, row.getRowNum());
							createCell(row, 1,obj.getMobile());
							createCell(row, 2, obj.getName());
							createCell(row, 3, DateUtils.format(obj.getTime(), "yyyy-MM-dd hh:mm:ss"));
							return row;
						}

						
					});
			return excelEntry.getKey();
	}
}
